package com.example.myactspringboot.server;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.example.myactspringboot.bean.Student;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Zhouchb
 * @Create 2024-04-10 20:59
 * @Description
 */
@Service
public class TemplateService {

    public void templateExport(HttpServletResponse response) {
        //获取数据
        List<Student> studentList = new ArrayList<>();
        Student student = new Student("1001","萧炎", 98.0);
        Student student1 = new Student("1003","林动", 99.0);
        Student student2 = new Student("1004","周元", 97.0);
        studentList.add(student);
        studentList.add(student1);
        studentList.add(student2);
        List<Map<String,Object>> dataList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("id","1001");
        map.put("name","xiaoyan");
        map.put("score",98);
        Map<String,Object> map1 = new HashMap<>();
        map1.put("id","1001");
        map1.put("name","lindong");
        map1.put("score",100);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id","1001");
        map2.put("name","zhouyuan");
        map2.put("score",98);
        dataList.add(map);
        dataList.add(map1);
        dataList.add(map2);
        try{
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("学生表.xlsx","utf-8"));

            //获取模板路径
//            String fullFilePath = fileProperties.getDir() + File.separatorChar + "成绩单.xls";
            InputStream in = this.getClass().getClassLoader().getResourceAsStream("excelTemplate/学生表.xlsx");

            //日期填充
            Map<String,String> date = new HashMap<>();
            date.put("date","20230331");

            //输出excel
            ExcelWriter build = EasyExcel.write(response.getOutputStream()).autoCloseStream(true).withTemplate(in).build();
                    build.fill(date, EasyExcel.writerSheet(0).build())
                    .fill(dataList, EasyExcel.writerSheet(0).build());

            WriteSheet dealerSheet = EasyExcel.writerSheet(1, "学生信息").registerWriteHandler(getCellStyle()).head(Student.class).build();
            build.write(studentList, dealerSheet);
                    build.finish();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private static HorizontalCellStyleStrategy getCellStyle() {
        //表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 背景设置
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setColor(IndexedColors.BLACK.getIndex());
        headWriteFont.setFontName("微软雅黑");
        headWriteFont.setFontHeightInPoints((short) 13);
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setTopBorderColor(IndexedColors.GREEN.getIndex());
        headWriteCellStyle.setBottomBorderColor(IndexedColors.GREEN.getIndex());
        headWriteCellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        headWriteCellStyle.setRightBorderColor(IndexedColors.GREEN.getIndex());
        //内容样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentFont = new WriteFont();
        contentFont.setFontHeightInPoints((short) 11);
//        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setWriteFont(contentFont);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBottomBorderColor(IndexedColors.GREEN.getIndex());
        contentWriteCellStyle.setTopBorderColor(IndexedColors.GREEN.getIndex());
        contentWriteCellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        contentWriteCellStyle.setRightBorderColor(IndexedColors.GREEN.getIndex());

        //设置内容靠左对齐
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
